<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of quanly
 *
 * @author Bui
 */
class Quanly_thanhvien extends CI_Controller {

    private $limit = 10;
    private $id_update = 0;

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('table');
        $this->load->library('form_validation');
        $this->load->model('quanly_thanhvien_model', '', TRUE);
    }

    function index($offset = 0) {
        // offset
        $uri_segment = 3;
        $offset = $this->uri->segment($uri_segment);

        // load data
        $data['result'] = $this->quanly_thanhvien_model->get_paged_list($this->limit, $offset)->result();

        // generate pagination
        $this->load->library('pagination');
        $config['base_url'] = site_url('quanly/quanly_thanhvien/index/');
        $config['total_rows'] = $this->quanly_thanhvien_model->count_all();
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        // generate table data
        $this->load->library('table');
        $this->table->set_empty("&nbsp;");
        $this->table->set_heading('ID', 'username', 'tenhienthi', 'gioitinh', 'email', 'sodienthoai', 'diachi', 'is7s', 'Xem', 'Sửa', 'Xóa');
        $i = 0 + $offset;
        foreach ($data['result'] as $row) {
            $this->table->add_row($row->id, $row->username, $row->tenhienthi, $row->gioitinh, $row->email, $row->sodienthoai, $row->diachi, 
                    $row->sodienthoai, 
                    anchor('quanly/quanly_thanhvien/view/' . $row->id, 'Xem', array('class' => 'view')), 
                    anchor('quanly/quanly_thanhvien/update/' . $row->id, 'Sửa', array('class' => 'update')), 
                    anchor('quanly/quanly_thanhvien/delete/' . $row->id, 'Xóa', array('class' => 'delete', 'onclick' => "return confirm('Are you sure want to delete this person?')"))
            );
        }
        $tmpl = array('table_open' => '<table class="table table-hover" color="#ecf0f1">');
        $this->table->set_template($tmpl);
        $data['table'] = $this->table->generate();

        // load view
        $this->load_html('admin/quanly/quanly_thanhvien/quanly_thanhvien_list', $data);
    }

    function load_html($path, $data) {
        $this->load->view('admin/quanly/common/header_view', $data);
        $this->load->view('admin/quanly/common/head_content_view', $data);
        $this->load->view($path, $data);
        $this->load->view('admin/quanly/common/tail_content_view', $data);
        $this->load->view('admin/quanly/common/footer_view', $data);
    }

    function add() {
        // set common properties
        $data['title'] = 'Add new data';
        $data['message'] = '';
        $data['action'] = site_url('quanly/quanly_thanhvien/addData');
        $data['link_back'] = anchor('quanly/quanly_thanhvien/index', 'Back to list of admin level', array('class' => 'back'));

        // load view
        $this->load_html('admin/quanly/quanly_thanhvien/quanly_thanhvien_add', $data);
    }

    function addData() {
        // set common properties
        $data['title'] = 'Add new data';
        $data['action'] = site_url('quanly/quanly_thanhvien/addData');
        $data['link_back'] = anchor('quanly/quanly_thanhvien/index', 'Back to list of admin level', array('class' => 'back'));
        // set validation properties
        $this->form_validation->set_rules('username', 'username', 'trim|required|min_length[6]|max_length[50]|xss_clean');

        // run validation
        if ($this->form_validation->run() == FALSE) {
            $data['message'] = 'Chưa tạo đc';
            $this->load_html('admin/quanly/quanly_thanhvien/quanly_thanhvien_add', $data);
        } else {
            // save data
            $data['message'] = 'Tạo đc';
            $data = array(
                'username' => $this->input->post('username'),
                'password' => $this->input->post('password'),
                'tenhienthi' => $this->input->post('tenhienthi'),
                'email' => $this->input->post('email'),
                'sodienthoai' => $this->input->post('sodienthoai'),
                'trangthai_id' => 1,
            );
            $id = $this->quanly_thanhvien_model->add($data);

            redirect('quanly/quanly_thanhvien/index', 'refresh');
        }
    }

    function view($id) {
        // set common properties
        $data['title'] = 'Chi tiết cấp bậc admin';
        $data['link_back'] = anchor('quanly/quanly_thanhvien/index', 'Back to list of admin level', array('class' => 'back'));

        // get person details
        $data['row'] = $this->quanly_thanhvien_model->get_by_id($id)->row();

        // load view
        $this->load_html('admin/quanly/quanly_thanhvien/quanly_thanhvien_view', $data);
    }

    function update($id) {
        // prefill form values
        $data['query'] = $this->quanly_thanhvien_model->get_data($id);
        // set common properties
        $data['title'] = 'Update admin level';
        $data['message'] = '';
        $data['action'] = site_url('quanly/quanly_thanhvien/updateData');
        $data['link_back'] = anchor('quanly/quanly_thanhvien/index', 'Back to list of admin level', array('class' => 'back'));

        // load view
        $this->load_html('admin/quanly/quanly_thanhvien/quanly_thanhvien_edit', $data);
    }

    function updateData($id) {
        // set common properties
        $data['title'] = 'Update admin level';
        $data['action'] = site_url('quanly/quanly_thanhvien/updateData');
        $data['link_back'] = anchor('quanly/quanly_thanhvien/index', 'Back to list of admin level', array('class' => 'back'));

        // set validation properties
        $this->form_validation->set_rules('username', 'username', 'trim|required|min_length[6]|max_length[50]|xss_clean');
        // run validation
        if ($this->form_validation->run() == FALSE) {
            $data['message'] = '';
            // load view
            $this->load_html('admin/quanly/quanly_thanhvien/quanly_thanhvien_edit', $data);
        } else {
            // save data

            $data = array(
                'username' => $this->input->post('username'),
                'tenhienthi' => $this->input->post('tenhienthi'),
                'email' => $this->input->post('email'),
                'sodienthoai' => $this->input->post('sodienthoai'),
            );

            $this->quanly_thanhvien_model->update($id, $data);

            // set user message
            $data['message'] = '<div class="success">update person success</div>';
            redirect('quanly/quanly_thanhvien/index', 'refresh');
        }
    }

    function delete($id) {
        // delete person
        $this->quanly_thanhvien_model->delete($id);

        // redirect to person list page
        redirect('quanly/quanly_thanhvien/index', 'refresh');
    }

}

?>
